import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import LoginView from '../views/LoginView';
import RegisterView from '../views/RegisterView';
import NotFoundView from '../views/NotFoundView';
import UserProfileView from '@/views/UserProfileView.vue';
import EventsView from '@/views/EventsView.vue';
import EventsEditorView from '@/views/EditorView/EventsEditorView.vue';
import EventsUpdateView from '@/views/EditorView/EventsUpdateView.vue';
import MaterialDownloadView from '@/views/MaterialDownloadView.vue';
import MaterialUploadView from '@/views/MaterialView/MaterialUploadView.vue';
import MaterialUpdateView from '@/views/MaterialView/MaterialUpdateView.vue';
import store from '@/store';

const routes = [
    {
        path: '/',
        name: 'home',
        component: HomeView,
        meta: {
            requestAuth: true,
        }
    },
    {
        path: '/userprofile/',
        name: 'userprofile',
        component: UserProfileView,
        meta: {
            requestAuth: true,
        }
    },
    {
        path: '/events/',
        name: 'events',
        component: EventsView,
        meta: {
            requestAuth: true,
        },
    },
    {
        path: '/MaterialDownload/',
        name: 'MaterialDownload',
        component: MaterialDownloadView,
        meta: {
            requestAuth: true,
        },
    },
    {
        path: '/MaterialUpload/',
        name: 'MaterialUpload',
        component: MaterialUploadView,
        meta: {
            requestAuth: true,
        }
    },
    {
        path: '/MaterialUpdate/:id',
        name: 'MaterialUpdate',
        component: MaterialUpdateView,
        meta: {
            requestAuth: true,
        }
    },
    {
        path: '/EventsEditor/',
        name: 'EventsEditor',
        component: EventsEditorView,
        meta: {
            requestAuth: true,
        }
    },
    {
        path: '/EventsUpdate/:id',
        name: 'EventsUpdate',
        component: EventsUpdateView,
        meta: {
            requestAuth: true,
        }
    },
    {
        path: '/login/',
        name: 'login',
        component: LoginView,
        meta: {
            requestAuth: false,
        }
    },
    {
        path: '/register/',
        name: 'register',
        component: RegisterView,
        meta: {
            requestAuth: false,
        }
    },
    {
        path: '/404/',
        name: '404',
        component: NotFoundView,
        meta: {
            requestAuth: false,
        }
    },
    
    // 重定向
    {
        path: '/:catchAll(.*)',
        redirect: "/404/"
    }
]

const router = createRouter({
    history: createWebHistory(),
    routes
})

// 每次使用router进入某一个页面之前，会先调用这个函数
// to表示跳转到哪个页面，from表示从哪个页面跳转过去的，next表示将页面要不要执行下一步操作
router.beforeEach((to, from, next) => {
    if (to.meta.requestAuth && !store.state.user.is_login) {
        next({name: "login"});
    } else {
        next();
    }
})

export default router
